package adv46.chat;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class FileSendThread extends Thread {
    private final Socket s;

    public FileSendThread(Socket s){
        this.s=s;
    }

    @Override
    public void run() {
        try {
            FileInputStream fin=new FileInputStream("D:\\Documents\\图片和资料\\2019.jpg");
            byte[]b=new byte[1024];
            int len=0;
            OutputStream out = s.getOutputStream();
            while ((len=fin.read(b))!=-1){
                out.write(b,0,len);
            }
            out.flush();
            fin.close();
            System.out.println("文件发送给"+s+"成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            System.out.println(s+"关闭了，断开连接");
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
